package Test;

import java.util.LinkedList;
import java.util.List;

public class 括号生成 {
    static List<String> list=new LinkedList<>();
    static List<String> generateParenthesis(int n){
        int left=n,right=n;
        StringBuffer track=new StringBuffer();
        back(n,n,track);
        return list;
    }
    static void back(int left,int right,StringBuffer track){
        if(left>right||left<0||right<0) return;
        if(left==0&&right==0) list.add(track.toString());
        track.append(')');
        back(left,right-1, track);
        track.deleteCharAt(track.length()-1);

        track.append('(');
        back(left-1,right,track);
        track.deleteCharAt(track.length()-1);
    }
    public static void main(String[] args) {
        System.out.println("generateParenthesis(3) = " + generateParenthesis(3));
    }

}
